package com.fdms.connectivity;


import java.sql.Connection;
import java.sql.SQLException;
import java.util.ResourceBundle;

import org.apache.commons.dbcp.cpdsadapter.DriverAdapterCPDS;
import org.apache.commons.dbcp.datasources.SharedPoolDataSource;


public class ConnectionHandler  {

    private static SharedPoolDataSource ds;
   // private static webSMSLog webLog;
    
    private ConnectionHandler() {
    	System.out.println("----- Constr -------");
    	createInstance();
    }

    static {
        createInstance();
    }

    public static void shutDownPool() {
        if (ds != null) {
            try {
               // webLog.info("Shutting down the conneciton POOL");
            	System.out.println("Shutting down the conneciton POOL");
                ds.close();
            } catch (Exception e) {
            	e.printStackTrace();
               // webLog.error(e);
            	System.out.println("Error: "+ e.getMessage());
            }
        }
    }

    public static void createInstance() {
        ResourceBundle r = ResourceBundle.getBundle("webfdms");
        DriverAdapterCPDS cpds = new DriverAdapterCPDS();
        shutDownPool();
       // webLog.info("Creating new Instance of Connection pool ");
        System.out.println("Creating new Instance of Connection pool ");
        try {
            cpds.setDriver(r.getString("driver"));
        } catch (ClassNotFoundException e) {
         //   webLog.error(e);
        	e.printStackTrace();
        	System.out.println("Error: "+ e.getMessage());
            return;
        }
        cpds.setUrl(r.getString("constring"));
        cpds.setUser(r.getString("user"));
        cpds.setPassword(r.getString("password"));
        
        int maxConnections = 30;
        int maxWait = 30;
        String validationQuery = r.getString("ValidationQuery");
        boolean testWhileIdle = false;
        //boolean removeAbandoned = false;
        
        try {
            maxConnections = new Integer(r.getString("MaxConnnection")).intValue();
            maxWait = new Integer(r.getString("MaxWait")).intValue();
            testWhileIdle = new Boolean(r.getString("TestWhileIdle")).booleanValue();
            validationQuery = r.getString("ValidationQuery");
        } catch (Exception e) {
            //webLog.error(e);
        	e.printStackTrace();
        	System.out.println("Error: "+ e.getMessage());
        }
        SharedPoolDataSource tds = new SharedPoolDataSource();
        tds.setConnectionPoolDataSource(cpds);
        tds.setMaxActive(maxConnections);
        tds.setMaxWait(maxWait);
        tds.setValidationQuery(validationQuery);
        tds.setTestWhileIdle(testWhileIdle);
        ds = tds;
        
        System.out.println("-------------");
        System.out.println(ds);
        System.out.println("-------------");
    }
    public static Connection getConnection() throws SQLException {
    	if(ds == null)
    		createInstance();
    	
        return ds.getConnection();
    }

    public static void freeConnection(Connection cn) throws SQLException {
        if (cn != null) {
            cn.close();
        }
    }

    public static void resetPool() throws SQLException {
        createInstance();
    }

}